<?php
namespace app\api\widget;

use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use yii\base\Widget;

use \doc;

class DocWidget extends Widget
{
    /**
     * @name 搜索的关键词
     * @var string
     */
    public $key = '';

    /**
     * @name 标记
     * @var string
     */
    public $flag = '';

    /**
     * @name 数据
     * @var array
     */
    public $data = [];

    /**
     * @name 路由：
     * @var string
     */
    public $dir = '';

    /**
     * @name 命名空间
     * @var string
     */
    public $namespace = '';

    /**
     * @name  缓存
     * @var bool
     */
    public $cache = false;

    /**
     * @name 选择方式
     * @var null
     */
    public $selected = null;
    /**
     * 查询数据
     * @return array
     */
    public function init() {

        parent::init();

        $this->selected = isset($_GET['a']) ? $_GET['a'] : 'help-auth';

        $key= isset($_GET['key']) ? $_GET['key'] : '';

        $doc = !$this->cache ? doc::$api : doc::$api->setCache();


        $this->data =  $doc->reflection($this->dir, $this->namespace, $key, $this->selected)->export();

        $this->data['template'] = in_array($this->selected, ['help-auth','help-response']) ? $this->selected : 'temp';
        $this->data['data'] = $this->data;
        return $this->data;
    }

    /**
     * 渲染界面
     * @return string
     */
    public function run()
    {
        $this->getView()->title = 'API文档说明';

        return $this->render('template', $this->data);
    }
}